package cn.edu.scau.sec.zhangsan.client;

import java.util.HashSet;
import java.util.Set;

import cn.edu.scau.sec.zhangsan.singleton.President;
import cn.edu.scau.sec.zhangsan.singleton.VicePresident;

public class SingletonClient {

	public static void main(String[] args) {
		testSingleton();
		testMultiton();
//		注意：有个问题，集合可以任意创建，但是，即使创建了更多的集合，但是创建副校长的时候这个集合被指向了VicePresident中的静态集合，这个集合的数量是被控制的。保证了副校长的数量。
		Set<VicePresident> v1 = new HashSet<VicePresident>();
		v1 = VicePresident.getVicePresident("a1");
		v1 = VicePresident.getVicePresident("a2");
		v1 = VicePresident.getVicePresident("a3");
		v1 = VicePresident.getVicePresident("a4");
		v1 = VicePresident.getVicePresident("a5");
		v1 = VicePresident.getVicePresident("a6");
		v1 = VicePresident.getVicePresident("a7");
		v1 = VicePresident.getVicePresident("a8");
		System.out.println("其他地方创建的副校长");
		printVicePresidents(v1);
		
		Set<VicePresident> v2 = new HashSet<VicePresident>();
		v2 = VicePresident.getVicePresident("b1");
		v2 = VicePresident.getVicePresident("b2");
		v2 = VicePresident.getVicePresident("b3");
		v2 = VicePresident.getVicePresident("b4");
		v2 = VicePresident.getVicePresident("b5");
		v2 = VicePresident.getVicePresident("b6");
		v2 = VicePresident.getVicePresident("b7");
		v2 = VicePresident.getVicePresident("b8");
		System.out.println("其他地方创建的副校长");
		printVicePresidents(v2);
		
		
	}



	private static void testSingleton() {
		President liuyahong = President.getPresident("刘雅红");
		System.out.println("你实际得到的校长是：" + liuyahong.getName());
		
		President chenxiaoyang = President.getPresident("陈晓阳");
		System.out.println("你实际得到的校长是：" + chenxiaoyang.getName());
		
		if(liuyahong == chenxiaoyang) {
			System.out.println("两次创建的校长实际是同一个对象");
		}else {
			System.out.println("两次创建的校长是不同的对象");
		}
	}
	
	
	private static void testMultiton() {
		//		当前我校副校长有7个，分别是温小波、咸春龙、杨运东、	邓诣群、	蒋育燕、	仇荣亮、	钟强
		
		Set<VicePresident> vicePresidents = new HashSet<VicePresident>(); 
				
		vicePresidents = VicePresident.getVicePresident("温小波");
		printVicePresidents(vicePresidents);
		VicePresident.getVicePresident("咸春龙");
		printVicePresidents(vicePresidents);
		VicePresident.getVicePresident("杨运东");
		printVicePresidents(vicePresidents);
		VicePresident.getVicePresident("邓诣群");
		printVicePresidents(vicePresidents);
		VicePresident.getVicePresident("蒋育燕");
		printVicePresidents(vicePresidents);
		VicePresident.getVicePresident("仇荣亮");
		printVicePresidents(vicePresidents);
		VicePresident.getVicePresident("钟强");
		printVicePresidents(vicePresidents);

		//		在已有的副校长的基础上添加其他的副校长，不能成功创建
		VicePresident.getVicePresident("张三");
		printVicePresidents(vicePresidents);
		VicePresident.getVicePresident("李四");
		printVicePresidents(vicePresidents);
	}

	private static void printVicePresidents(Set<VicePresident> vicePresidents) {
		System.out.print("当前创建的副校长的名单是：");
		for(VicePresident vicePresident: vicePresidents) {
			System.out.print(vicePresident.getName()+";");
		}
		System.out.println();
		
	}
	
	

}
